package com.ggh.jinjilive.ui.mall.shop.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemPendingBinding;
import com.ggh.jinjilive.databinding.ItemPendingTaskBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener;
import com.ggh.jinjilive.mobile.mall.shop.MineShopOrder;
import com.ggh.jinjilive.mobile.mall.shop.MineShopOrderTask;

import java.util.List;

public class PendingTaskAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<MineShopOrderTask> mineShopOrderTaskList;

    public void setAdd(List<MineShopOrderTask> mineShopOrderTaskList) {
        this.mineShopOrderTaskList = mineShopOrderTaskList;
        notifyDataSetChanged();
    }

    public void ADD(List<MineShopOrderTask> mineShopOrderTaskList) {
        if(null !=  this.mineShopOrderTaskList){
            this.mineShopOrderTaskList.addAll(mineShopOrderTaskList);
        }else {
            this.mineShopOrderTaskList = mineShopOrderTaskList;
        }
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemPendingTaskBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_pending_task, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
       if(recyclerViewholder instanceof ItemViewHolder){
            ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
            MineShopOrderTask mineShopOrderTask = mineShopOrderTaskList.get(position);
            if (null != mineShopOrderTask) {
                holder.binding.llAddTask.setVisibility(View.GONE);
                holder.binding.llTaskMessage.setVisibility(View.VISIBLE);
            }else {
                holder.binding.llAddTask.setVisibility(View.VISIBLE);
                holder.binding.llTaskMessage.setVisibility(View.GONE);
                holder.binding.llAddTask.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(99999999,"添加");
                        }
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return null == mineShopOrderTaskList ? 0 : mineShopOrderTaskList.size();
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemPendingTaskBinding binding;
        public ItemViewHolder(@NonNull ItemPendingTaskBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}